//
// See the file COPYING for redistribution information.
//

#ifndef __NAIPAX_URL_HPP__
#define __NAIPAX_URL_HPP__

#include <stdint.h>
#include <string>

namespace Naipax {
    /** URL processing
     *
     * This is a specialized URL processing module, without
     * implementing the full URL spec. This module uses a simple
     * regular expression to match user data, and fragments are always
     * discarded.
     */
    class Url {
    public:
        /// Constructor
        Url();
        
        /** Constructor
         *
         * @param rUrl URL
         */
        Url(const std::string &rUrl);

        /** Constructor
         *
         * @param pUrl URL
         */
        Url(const char *pUrl);

        /// Destructor
        ~Url();

        /// Set URL content
        void Set(const std::string &rUrl);

        /// Set URL content
        void Set(const char *pUrl);
        
        /** Return if URL is valid
         *
         * Please check if URL is valid after calling constructor.
         */
        bool IsValid() {
            return _mIsValid;
        }

        /** Return the depth of path
         *
         * Actually, this method does nothing more than counting the
         * number of slashes in _mPath. The trailing slash is not
         * taken into counting.
         */
        uint32_t PathDepth();

        /** Return a substring composed of multiple domain levels
         * E.g. The sub hostname of ("www.wikipedia.org", 2) is
         * "wikipedia.org"; ("http://snakesevra.deviantart.com", 2
         *
         * @param Level
         * @return empty string if Level exceeds DomainLevels().
         * @return Subhostname otherwise
         */
        std::string SubHostname(uint32_t Level);
        
        /** Normalize URL
         *
         * Please use AsString() to get normalized results. 
         */
        void Normalize();

        /** Return URL
         *
         * @return The original URL if Normalize() is not called.
         * @return Normalized URL if Normalized is called.
         * @return Empty string if IsValid() does not return true.
         */
        std::string& AsString();

        /** Return the number of levels of URL's host name
         *
         * E.g. The level of "www.wikipedia.org" is 3; "google.com",
         * 2.
         */
        uint32_t DomainLevels() {
            return _mDomainLevels;
        }

        /// Getter/Setter of _mScheme
        std::string& Scheme() { 
            return _mScheme;
        }

        /// Getter/Setter of _mAuthority
        std::string& Authority() { 
            return _mAuthority;
        }

        /// Getter/Setter of _mHost
        std::string& Host() { 
            return _mHost;
        }

        /// Getter/Setter of _mPort
        std::string& Port() { 
            return _mPort;
        }

        /// Getter/Setter of _mPath
        std::string& Path() { 
            return _mPath;
        }

        /// Getter/Setter of _mQuery
        std::string& Query() { 
            return _mQuery;
        }

    private:
        bool _mIsValid;
        std::string _mUrl;
        std::string _mScheme;
        std::string _mAuthority;
        std::string _mHost;
        std::string _mPort;
        std::string _mPath;
        std::string _mQuery;
        uint32_t _mDomainLevels;
    };
};

#endif // __NAIPAX_URL_HPP__
